package com.bidevalution.business.impl;

import com.bidevalution.business.service.*;
import com.bidevalution.dao.entity.BeNoticeTemplate;
import com.bidevalution.dao.entity.BeSectionParticipant;
import com.bidevalution.support.context.NoticeTemplateCode;
import com.bidevalution.support.context.SectionStateEnum;
import com.bidevalution.support.factory.NoiceDataFactory;
import com.chatroom.api.dto.ChatGroupInfoDTO;
import com.chatroom.api.dto.ChatNoticeDataDTO;
import com.chatroom.api.service.ChatGroupInfoApiService;
import com.chatroom.api.service.ChatNoticeDataApiService;
import com.qtp.core.redis.RedisUtil;
import com.qtp.core.util.DateUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BeNoticeDataServiceImpl implements BeNoticeDataService{
	@Autowired
	private ChatNoticeDataApiService chatNoticeDataApiService;
	@Autowired
	private BeNoticeTemplateService beNoticeTemplateService;
	@Autowired
	private BeSectionSignService beSectionSignService;
	@Autowired
	private BeSectionParticipantService beSectionParticipantService;
	@Autowired
	private BeSectionDecryptService beSectionDecryptService;
	@Autowired
	private ChatGroupInfoApiService chatGroupInfoApiService;
	@Value("${SystemIdentify}")
	private String appKey;
	private Logger log = Logger.getLogger(getClass());
	public String getGroupId(String sectionId) {
		ChatGroupInfoDTO chatGroupInfoDTO = chatGroupInfoApiService.getByBusinessGroupId(appKey, sectionId);
		if(chatGroupInfoDTO == null) {
			log.error("groupId is null,sectionId is:"+sectionId);
            return null;
		}
		return chatGroupInfoDTO.getGroupId();
	}
	public void addC4(String sectionId,Date messageTime){
		addNoticeData(NoticeTemplateCode.C1004, getGroupId(sectionId), new NoiceDataFactory() {
			@Override
			public void add(String groupId,String templateContent) throws Exception {
				chatNoticeDataApiService.add(templateContent,
						messageTime,
						groupId, NoticeTemplateCode.C1004.name(),false);
			}
		});
	}

	@Override
	public void addC6(String sectionId,Date messageTime,Integer signCount, Integer finishDecryptCount, Integer unFinishDecryptCount) {
		if (!NoticeTemplateCode.C1006.name().equals(RedisUtil.get( getGroupId(sectionId)+ NoticeTemplateCode.C1006.name()))) {
			addNoticeData(NoticeTemplateCode.C1006, getGroupId(sectionId), new NoiceDataFactory() {
				@Override
				public void add(String groupId,String templateContent) throws Exception {
					templateContent = templateContent.replace("{signCount}", signCount + "");
					templateContent = templateContent.replace("{finishDecryptCount}", finishDecryptCount + "");
					templateContent = templateContent.replace("{unFinishDecryptCount}", unFinishDecryptCount + "");
					chatNoticeDataApiService.add(templateContent, messageTime,
							groupId, NoticeTemplateCode.C1006.name(),false);
					//放入Redis缓存
					RedisUtil.set(groupId + NoticeTemplateCode.C1006.name(), NoticeTemplateCode.C1006.name());
					//设置key值24小时过期
					RedisUtil.expire(groupId + NoticeTemplateCode.C1006.name(), 24 * 60 * 60);
				}
			});
		}
		
	}

	@Override
	public void addC7(String sectionId,Date messageTime,Integer signCount, Integer finishDecryptCount) {
		if (!NoticeTemplateCode.C1007.name().equals(RedisUtil.get(getGroupId(sectionId) + NoticeTemplateCode.C1007.name()))) {

			addNoticeData(NoticeTemplateCode.C1007, getGroupId(sectionId), new NoiceDataFactory() {
				@Override
				public void add(String groupId,String templateContent) throws Exception {
					templateContent = templateContent.replace("{signCount}", signCount + "");
					templateContent = templateContent.replace("{finishDecryptCount}", finishDecryptCount + "");
					chatNoticeDataApiService.add(templateContent, messageTime,
							groupId, NoticeTemplateCode.C1007.name(),false);
					//放入Redis缓存
					RedisUtil.set(groupId + NoticeTemplateCode.C1007.name(), NoticeTemplateCode.C1007.name());
					//设置key值24小时过期
					RedisUtil.expire(groupId + NoticeTemplateCode.C1007.name(), 24 * 60 * 60);
				}
			});
		}
	}

	@Override
	public void addC8(String sectionId,Date messageTime) {
		addNoticeData(NoticeTemplateCode.C1008, getGroupId(sectionId), new NoiceDataFactory() {
			@Override
			public void add(String groupId,String templateContent) throws Exception {
				chatNoticeDataApiService.add(templateContent,messageTime,
						groupId,NoticeTemplateCode.C1008.name(),false);

			}
		});
	}

	@Override
	public void addC9(String sectionId,Date messageTime,Integer signCount, Integer finishDecryptCount, Integer unFinishDecryptCount) {
		addNoticeData(NoticeTemplateCode.C1009, getGroupId(sectionId), new NoiceDataFactory() {
			@Override
			public void add(String groupId,String templateContent) throws Exception {
				
				chatNoticeDataApiService.add(templateContent,messageTime,
						groupId,NoticeTemplateCode.C1009.name(),false);

			}
		});
	}

	@Override
	public void addC10(String sectionId,Date messageTime) {
		if (!NoticeTemplateCode.C1010.name().equals(RedisUtil.get(getGroupId(sectionId) + NoticeTemplateCode.C1010.name()))) {
			addNoticeData(NoticeTemplateCode.C1010, getGroupId(sectionId), new NoiceDataFactory() {
				@Override
				public void add(String groupId,String templateContent) throws Exception {

					chatNoticeDataApiService.add(templateContent, messageTime,
							groupId, NoticeTemplateCode.C1010.name(),false);
					//放入Redis缓存
					RedisUtil.set(groupId + NoticeTemplateCode.C1010.name(), NoticeTemplateCode.C1010.name());
					//设置key值24小时过期
					RedisUtil.expire(groupId + NoticeTemplateCode.C1010.name(), 24 * 60 * 60);
				}
			});
		}
	}

	@Override
	public void addC11(String sectionId,Date messageTime) {
		addNoticeData(NoticeTemplateCode.C1011, getGroupId(sectionId), new NoiceDataFactory() {
			@Override
			public void add(String groupId,String templateContent) throws Exception {
				
				chatNoticeDataApiService.add(templateContent,messageTime,
						groupId,NoticeTemplateCode.C1011.name(),false);

			}
		});
	}

	@Override
	public void addC12(String sectionId,Date messageTime) {
		if (!NoticeTemplateCode.C1012.name().equals(RedisUtil.get(getGroupId(sectionId) + NoticeTemplateCode.C1012.name()))) {
			addNoticeData(NoticeTemplateCode.C1012, getGroupId(sectionId), new NoiceDataFactory() {
				@Override
				public void add(String groupId,String templateContent) throws Exception {
					chatNoticeDataApiService.add(templateContent, messageTime,
							groupId, NoticeTemplateCode.C1012.name(),false);
					//放入Redis缓存
					RedisUtil.set(groupId + NoticeTemplateCode.C1012.name(), NoticeTemplateCode.C1012.name());
					//设置key值24小时过期
					RedisUtil.expire(groupId + NoticeTemplateCode.C1012.name(), 24 * 60 * 60);
				}
			});
		}
	}

	@Override
	public void addC13(String sectionId,Date messageTime,String... companyNames) {
		addNoticeData(NoticeTemplateCode.C1013,getGroupId(sectionId), new NoiceDataFactory() {
			@Override
			public void add(String groupId,String templateContent) throws Exception {
				int i=1;
				StringBuilder candidates = new StringBuilder();
				for(String company:companyNames) {
					candidates.append("第").append(i).append("中标候选人为【").append(company).append("】");
					i++;
				}
				templateContent = templateContent.replace("{candidates}",candidates.toString());
				chatNoticeDataApiService.add(templateContent,messageTime,
						groupId,NoticeTemplateCode.C1013.name(),false);

			}
		});
	}

	public void addNoticeData(NoticeTemplateCode noticeTemplateCode, String groupId,
			NoiceDataFactory noiceDataFactory) {
		if(groupId == null) {
			log.error("groupId is null:"+groupId);
			return;
		}
		List<ChatNoticeDataDTO> chatNoticeDataDTOs = chatNoticeDataApiService.selectListByCode(noticeTemplateCode.name(), groupId);
		
		BeNoticeTemplate beNoticeTemplate = beNoticeTemplateService.selectByPrimaryKey(noticeTemplateCode.name());
		if (beNoticeTemplate != null && chatNoticeDataDTOs.size() == 0) {
			try {
				noiceDataFactory.add(groupId,beNoticeTemplate.getContent());
			} catch (Exception e) {
				// TODO Auto-generated catch block
                log.error(e.getMessage(), e);
			}
		}
	}

	@Override
	public void addNotice(String sectionId, String sectionStage) throws Exception  {
		if(SectionStateEnum.decrypting.getValue().equals(sectionStage)) {
			//进入解密阶段
			addC4(sectionId, DateUtil.now());
		} else if(SectionStateEnum.bided.getValue().equals(sectionStage)) {
			//进入开标结果
			addC8(sectionId, DateUtil.now());
		} else if(SectionStateEnum.paramdrawing.getValue().equals(sectionStage)) {
			int finishDecryptCount = beSectionDecryptService.getDecryptedCount(sectionId).intValue();
			int signCount = beSectionSignService.querySignCount(sectionId).intValue();
			int unFinishDecryptCount = signCount - finishDecryptCount;
			//进入参数抽取
			addC9(sectionId, DateUtil.now(),signCount,finishDecryptCount,unFinishDecryptCount);
		} else if(SectionStateEnum.reviewing.getValue().equals(sectionStage)) {
			//进入评审
			addC11(sectionId, DateUtil.now());
		} else if(SectionStateEnum.finished.getValue().equals(sectionStage)) {
			List<BeSectionParticipant> beSectionParticipants = beSectionParticipantService.getCandidateList(sectionId);
			if(beSectionParticipants != null && beSectionParticipants.size() > 0){
				String[] companyNames = new String[beSectionParticipants.size()];
				for(int i=0; i<beSectionParticipants.size(); i++){
					companyNames[i] = beSectionParticipants.get(i).getParticipantName();
				}
				//进入结束页面
				addC13(sectionId, DateUtil.now(), companyNames);
			}
		}
	}

	@Override
	public void addC14(String sectionId, Date messageTime) {
		addNoticeData(NoticeTemplateCode.C1014, getGroupId(sectionId), new NoiceDataFactory() {
			@Override
			public void add(String groupId,String templateContent) throws Exception {
				
				chatNoticeDataApiService.add(templateContent,messageTime,
						groupId,NoticeTemplateCode.C1014.name(),false);

			}
		});
	}
}
